package br.edu.sistemaacademico.builder;



import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import br.edu.sistemaacademico.builder.impl.DeleteSQLBuilder;
import br.edu.sistemaacademico.builder.impl.InsertSQLBuilder;
import br.edu.sistemaacademico.builder.impl.SelectSQLBuilder;
import br.edu.sistemaacademico.builder.impl.UpdateSQLBuilder;
import br.edu.sistemaacademico.dao.DaoGenerico;
import br.edu.sistemaacademico.facade.DataFacade;
import br.edu.sistemaacademico.pojo.Aluno;
import br.edu.sistemaacademico.pojo.Curso;
import br.edu.sistemaacademico.pojo.Matricula;
import br.edu.sistemaacademico.pojo.Servidor;

public class MatriculaDAO extends DaoGenerico {

	public static final List<String> COLUNAS = Arrays
			.asList(new String[] { "codigo", "dataMatricula",
					"estado", "aluno", "curso" });

	public List<Object> getCampos(){
		return (List) COLUNAS;
	}
	@Override
	public void alterar(Object o) throws SQLException {
		Matricula m = (Matricula) o;
		List<Object> valores = Arrays.asList(new Object[] { m.getId(),
				m.getDataMatricula(), m.getEstado(),
				m.getAluno().getId(), m.getCurso().getId() });

		String condicao = "codigo = '" + m.getId() + "'";
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_HISTORICO");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	

	@Override
	public Object getObject(String condicao, String tabelasAdicionais) throws SQLException {
		Matricula m = new Matricula();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_MATRICULA a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.setCondicao(condicao);
		select.colunasToReturn = "a.*";

		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				m = (Matricula) informacoes(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return m;
	}

	public List<Object> getAll(String condicao, String tabelasAdicionais, String ordenacao){
		List<Matricula> list = new LinkedList<Matricula>();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_MATRICULA a"+tabelasAdicionais);
		select.setColunas(COLUNAS);		
		select.setCondicao(condicao);
		select.colunasToReturn = "a.*";
		select.orderBy = ordenacao;
		
		
		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			while (rs.next())
				list.add((Matricula) informacoes(rs));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (List) list;
	}

	@Override
	public void gravar(Object o) throws SQLException {
		DataFacade facade = new DataFacade();
		Matricula m = (Matricula) o;
		System.out.println(m.getAluno().getId());
		List<Object> valores = Arrays.asList(new Object[] { m.getId(),
				facade.getStingGravar(m.getDataMatricula()), m.getEstado(),
				m.getAluno().getId(), m.getCurso().getId() });
		
		SQLDirector dir = new SQLDirector();
		InsertSQLBuilder insert = new InsertSQLBuilder();

		insert.setNomeTabela("tb_MATRICULA");
		insert.setColunas(COLUNAS);
		insert.setValores(valores);

		dir.buildSQL(insert);
		try {
			executeCommand(insert.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Object informacoes(ResultSet rs) throws SQLException {
		Matricula a = new Matricula();
		AlunoDAO alunoDao = new AlunoDAO();
		CursoDAO cursoDao = new CursoDAO();
		a.setAluno((Aluno)alunoDao.getObject("codigo="+rs.getLong(COLUNAS.get(3)),""));
		a.setCurso((Curso)cursoDao.getObject("codigo="+rs.getLong(COLUNAS.get(4)),""));
		a.setId(rs.getLong(COLUNAS.get(0)));
		a.setDataMatricula(rs.getDate(COLUNAS.get(1)));
		a.setEstado(rs.getString(COLUNAS.get(2)));
		//a.getAluno().setId(rs.getLong(COLUNAS.get(3)));
		//a.getCurso().setId(rs.getLong(COLUNAS.get(4)));
		return a;
	}

	@Override
	public void remover(Object o) throws SQLException {
		Matricula m = (Matricula) o;
		List<Object> valores = Arrays.asList(new Object[] { m.getId(),
				m.getDataMatricula(), m.getEstado(),
				m.getAluno().getId(), m.getCurso().getId() });
		
		SQLDirector dir = new SQLDirector();
		DeleteSQLBuilder delete = new DeleteSQLBuilder();

		delete.setNomeTabela("tb_Matricula");
		delete.setColunas(COLUNAS);
		delete.setValores(valores);
		dir.buildSQL(delete);
		try {
			executeCommand(delete.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
